package com.ztgf.shopping.service.shop;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.DateUtils;
import com.ztgf.message.model.bo.MessageAnnouncementSaveBO;
import com.ztgf.message.model.constant.MessageTopicType;
import com.ztgf.message.model.dto.ResponseDTO;
import com.ztgf.message.model.enums.MessageEnum;
import com.ztgf.message.model.service.IKafkaProducerService;
import com.ztgf.pay.models.bo.PayOrderMotherSaveBO;
import com.ztgf.pay.models.bo.PayUserMoneySaveBO;
import com.ztgf.pay.models.bo.PayUserRechargeSaveBO;
import com.ztgf.pay.models.dto.PayUserRechargeDTO;
import com.ztgf.pay.models.service.IPayOrderRepayService;
import com.ztgf.pay.models.service.IPayOrderService;
import com.ztgf.pay.models.service.IPayUserRechargeService;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.shop.ShopEarnestMoneyMapper;
import com.ztgf.shopping.models.bo.shop.ShopEarnestMoneyDeductionBO;
import com.ztgf.shopping.models.bo.shop.ShopEarnestMoneyReturnBO;
import com.ztgf.shopping.models.bo.shop.ShopEarnestMoneySaveBO;
import com.ztgf.shopping.models.bo.shop.ShopPayAmountLogSaveBO;
import com.ztgf.shopping.models.constant.ShoppingConstant;
import com.ztgf.shopping.models.dto.shop.ShopEarnestMoneyDTO;
import com.ztgf.shopping.models.dto.shop.ShopEarnestMoneyLogDTO;
import com.ztgf.shopping.models.dto.shop.ShopInfoDTO;
import com.ztgf.shopping.models.dto.shop.ShopPayAmountLogDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.shop.IShopEarnestMoneyService;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.sms.models.bo.SmsInfoSaveBO;
import com.ztgf.sms.models.service.ISmsInfoService;
import com.ztgf.sys.models.dto.SysDictDTO;
import com.ztgf.sys.models.service.ISysDictService;
import com.ztgf.user.models.dto.UserLoginDTO;
import com.ztgf.user.models.service.IUserLoginService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 店铺保证金控制器
 *
 * @author zhanghualang
 * @Date 2020-06-16 14:38:24
 */
@DubboService
public class ShopEarnestMoneyServiceImpl extends ServiceImpl<ShopEarnestMoneyMapper, ShopEarnestMoneyDTO> implements IShopEarnestMoneyService{

    @Resource
    private YmlConfig ymlConfig;
    @Autowired
    @Lazy
    private IShopInfoService shopInfoService;
    @Autowired
    private ShopEarnestMoneyLogServiceImpl iShopEarnestMoneyLogService;
    @Autowired
    private ShopPayAmountLogServiceImpl shopPayAmountLogService;
    @DubboReference
    private IPayUserRechargeService iPayUserRechargeService;
    @DubboReference
    private IPayOrderRepayService iPayOrderRepayService;
    @DubboReference
    private IPayOrderService iPayOrderService;
    @DubboReference
    private IUserLoginService iUserLoginService;
    @DubboReference
    private ISysDictService iSysDictService;
    @Autowired
    private EarnestMoneyTimerTaskService earnestMoneyTimerTaskService;
    @DubboReference
    private ISmsInfoService iSmsInfoService;
    @DubboReference
    private IKafkaProducerService kafkaProducerService;

    @Override
    public boolean add(ShopEarnestMoneySaveBO saveBO) {
        ShopEarnestMoneyDTO dto = new ShopEarnestMoneyDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        if (dto.getStatus().equals(0)){
            //开启年费倒计时线程
            SysDictDTO sysDictDTO = iSysDictService.getByName(ShoppingConstant.ANNUAL_FEE_TIME);//年费倒计时(适用于保证金)
            String time = sysDictDTO.getValue();
            earnestMoneyTimerTaskService.createOrderCountDown(Integer.valueOf(time), id.toString());//15天倒计时后关闭店铺
            Date expireTime = DateUtils.addDay(DateUtils.getCurrentDateTime(), Integer.valueOf(time));
            dto.setExpiration(expireTime);
            dto.setCountdownStatus(1);//倒计时状态 0未启动，1启动中，2已过期
        }
        return this.insert(dto);
    }

    @Override
    public ShopEarnestMoneyDTO getByShopId(Long id) {
        Wrapper<ShopEarnestMoneyDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id", id);

        return this.selectOne(wrapper);
    }

    @Override
    public boolean updatePayable(Long id, BigDecimal payable) {
        //debug 事务
        ShopEarnestMoneyDTO shopEarnestMoneyDTO = this.selectById(id);
        ShopEarnestMoneyDTO dto = new ShopEarnestMoneyDTO();
        ShopInfoDTO shopInfoDTO = new ShopInfoDTO();
        dto.setId(id);
        dto.setPayable(payable);//应交
        if(payable.compareTo(BigDecimal.ZERO) <= 0){
            //若设置的应缴金额小于0
            dto.setStatus(1); //状态 0未缴纳，1已缴纳，2保证金不足
            dto.setVacancyAmount(new BigDecimal("0.00"));//需补足保证金
            shopInfoDTO.setMarginStatus(1);//保证金状态 0未缴纳，1已缴纳，2保证金不足 defaultValue 0
            earnestMoneyTimerTaskService.remove(id.toString());
        }else{
            if (shopEarnestMoneyDTO.getBalance().compareTo(payable) >= 0){
                //若剩余保证金大于或等于应缴。则不需要缴纳
                dto.setStatus(1); //状态 0未缴纳，1已缴纳，2保证金不足
                dto.setVacancyAmount(new BigDecimal("0.00"));//需补足保证金
                dto.setCountdownStatus(0);//倒计时状态 0未启动，1启动中，2已过期
                shopInfoDTO.setMarginStatus(1);//保证金状态 0未缴纳，1已缴纳，2保证金不足 defaultValue 0
                //杀掉保证金倒计时进程
                earnestMoneyTimerTaskService.remove(id.toString());
            }else{
                //开启年费倒计时线程
                SysDictDTO sysDictDTO = iSysDictService.getByName(ShoppingConstant.ANNUAL_FEE_TIME);//年费倒计时(适用于保证金)
                String time = sysDictDTO.getValue();
                earnestMoneyTimerTaskService.createOrderCountDown(Integer.valueOf(time), id.toString());//15天倒计时后关闭店铺
                Date expireTime = DateUtils.addDay(DateUtils.getCurrentDateTime(), Integer.valueOf(time));
                dto.setExpiration(expireTime);
                dto.setCountdownStatus(1);//倒计时状态 0未启动，1启动中，2已过期
                dto.setStatus(0); //状态 0未缴纳，1已缴纳，2保证金不足
                dto.setVacancyAmount(payable.subtract(shopEarnestMoneyDTO.getBalance()));//需补足保证金 应缴-剩余
                shopInfoDTO.setMarginStatus(0);//保证金状态 0未缴纳，1已缴纳，2保证金不足 defaultValue 0
            }
        }
        shopInfoDTO.setId(shopEarnestMoneyDTO.getShopId());
        shopInfoService.updateById(shopInfoDTO);

        return this.updateById(dto);
    }

    @Override
    public Result updateDeductionEarnest(ShopEarnestMoneyDeductionBO bo) {
        //debug 事务
        /**
         * 1,修改保证金，相关信息
         * 店铺保证金修改
         * 2.增加保证金使用日志
         */
        ShopEarnestMoneyDTO dto = this.selectById(bo.getId());
        //判断余额
        if (dto.getBalance().compareTo(bo.getMoney()) < 0){
            return new Result<>(CodeEnum.FAIL_200028);
        }

        //中金扣除保证金
        PayOrderMotherSaveBO saveBO = new PayOrderMotherSaveBO();
        saveBO.setPaymentUserId(dto.getShopId());
        saveBO.setMoney(bo.getMoney());
        Long serialNumber = ymlConfig.getPkId();
        StringBuffer str = new StringBuffer("[{");
        str.append("paymentUserId: " + dto.getShopId() + ","); //付款方id
        str.append("bankId: \"\","); //银行ID
        str.append("bankName: \"\","); //银行名
        str.append("money: " + bo.getMoney() + ","); //付款金额
        str.append("businessOrderNo: \"" + serialNumber + "\","); //业务订单号
        str.append("businessOrderWaterNo: \"" + serialNumber + "\","); //业务订单流水号
        str.append("receiveUserId: " + dto.getShopId() + ","); //收款方id
        str.append("moneyUsage: \"余额-扣除保证金\","); //资金用途
        str.append("commodityMsg: \"余额-扣除保证金\""); //商品信息
        str.append("}]");

        saveBO.setChildOrderList(str.toString());
        saveBO.setTrsFlag("B01");
//        saveBO.setPayType(5);

        Result result = iPayOrderService.saveBalancePay(saveBO);

        if(result.getCode() != 0){
            return result;
        }

        ShopEarnestMoneyDTO shopEarnestMoneyDTO = new ShopEarnestMoneyDTO();
        shopEarnestMoneyDTO.setId(bo.getId());
        shopEarnestMoneyDTO.setUpdateUserId(bo.getCreateUserId());
        shopEarnestMoneyDTO.setBalance(dto.getBalance().subtract(bo.getMoney()));//余额
        //debug 阈值判断 冻结店铺
        shopEarnestMoneyDTO.setVacancyAmount(dto.getPayable().subtract(shopEarnestMoneyDTO.getBalance()));//需补足
        shopEarnestMoneyDTO.setUsedAmount(dto.getUsedAmount().add(bo.getMoney())); //已使用金额
        shopEarnestMoneyDTO.setStatus(2); //状态 0未缴纳，1已缴纳，2保证金不足
        this.updateById(shopEarnestMoneyDTO);

        //开启保证金倒计时线程
        SysDictDTO sysDictDTO = iSysDictService.getByName(ShoppingConstant.ANNUAL_FEE_TIME);//年费倒计时(适用于保证金)
        String time = sysDictDTO.getValue();
        earnestMoneyTimerTaskService.createOrderCountDown(Integer.valueOf(time), bo.getId().toString());//15天倒计时后关闭店铺
        Date expireTime = DateUtils.addDay(DateUtils.getCurrentDateTime(), Integer.valueOf(time));
        dto.setExpiration(expireTime);
        dto.setCountdownStatus(1);//倒计时状态 0未启动，1启动中，2已过期

        //店铺保证金修改
        ShopInfoDTO shopInfoDTO = new ShopInfoDTO();
        shopInfoDTO.setEarnestMoney(shopEarnestMoneyDTO.getBalance());
        shopInfoDTO.setId(dto.getShopId());
        shopInfoDTO.setMarginStatus(2); //状态 0未缴纳，1已缴纳，2保证金不足
        shopInfoService.updateById(shopInfoDTO);

        //增加保证金使用日志
        ShopEarnestMoneyLogDTO shopEarnestMoneyLogDTO = new ShopEarnestMoneyLogDTO();
        shopEarnestMoneyLogDTO.setMoney(bo.getMoney());
        shopEarnestMoneyLogDTO.setId(ymlConfig.getPkId());
        shopEarnestMoneyLogDTO.setNotes(bo.getNotes());
        shopEarnestMoneyLogDTO.setShopId(dto.getShopId());
        shopEarnestMoneyLogDTO.setType(1);//类型 0缴纳、1扣除、2退回
        shopEarnestMoneyLogDTO.setCreateUserId(bo.getCreateUserId());
        iShopEarnestMoneyLogService.insert(shopEarnestMoneyLogDTO);

        andSms(dto.getShopId());//发送短信
        urgeSignContact("因您涉及违约，您的保证金被平台进行扣罚", dto.getShopId(), 1);//发送站内信
        return new Result<>();
    }

    @Override
    public void updatePayEarnest(ShopEarnestMoneyDeductionBO bo) {
        /**
         * 1,修改保证金，相关信息
         * 店铺保证金修改
         * 2.增加保证金使用日志
         */
        ShopInfoDTO shopInfoDTO = new ShopInfoDTO();
        shopInfoDTO.setId(bo.getShopId());

        ShopEarnestMoneyDTO shopEarnestMoneyDTO = new ShopEarnestMoneyDTO();
        ShopEarnestMoneyDTO oldDto = getByShopId(bo.getShopId());
        shopEarnestMoneyDTO.setId(oldDto.getId());
        shopEarnestMoneyDTO.setPaid(oldDto.getPaid().add(bo.getMoney()));//已缴纳
        shopEarnestMoneyDTO.setBalance(oldDto.getBalance().add(bo.getMoney()));//余额
        if (shopEarnestMoneyDTO.getBalance().compareTo(oldDto.getPayable()) >= 0){
            //若余额大于等于应缴。则已缴纳
            //debug  店铺取消冻结
            shopEarnestMoneyDTO.setStatus(1); //状态 0未缴纳，1已缴纳，2保证金不足
            shopEarnestMoneyDTO.setVacancyAmount(new BigDecimal("0.00"));//需补足保证金
            shopInfoDTO.setMarginStatus(1);//保证金状态 0未缴纳，1已缴纳，2保证金不足 defaultValue 0
        }else {
            shopEarnestMoneyDTO.setStatus(2); //状态 0未缴纳，1已缴纳，2保证金不足
            shopEarnestMoneyDTO.setVacancyAmount(oldDto.getPayable().subtract(shopEarnestMoneyDTO.getBalance()));//需补足保证金 应缴-剩余
            shopInfoDTO.setMarginStatus(2);//保证金状态 0未缴纳，1已缴纳，2保证金不足 defaultValue 0
        }

        shopInfoDTO.setEarnestMoney(shopEarnestMoneyDTO.getBalance());
        shopInfoService.updateById(shopInfoDTO);
        this.updateById(shopEarnestMoneyDTO);

        //添加日志
        //增加保证金使用日志
        ShopEarnestMoneyLogDTO shopEarnestMoneyLogDTO = new ShopEarnestMoneyLogDTO();
        shopEarnestMoneyLogDTO.setMoney(bo.getMoney());
        shopEarnestMoneyLogDTO.setId(ymlConfig.getPkId());
        shopEarnestMoneyLogDTO.setShopId(bo.getShopId());
        shopEarnestMoneyLogDTO.setType(0);//类型 0缴纳、1扣除、2退回
        shopEarnestMoneyLogDTO.setCreateUserId(bo.getCreateUserId());
        iShopEarnestMoneyLogService.insert(shopEarnestMoneyLogDTO);
    }

    @Override
    public Result updatePaidByBalance(Long id, String mobile, String captcha) {
        ShopEarnestMoneyDTO dto = this.selectById(id);
        if (dto == null || dto.getStatus().equals(1) || dto.getVacancyAmount().compareTo(BigDecimal.ZERO) == 0){
            return new Result(CodeEnum.FAIL_200023);
        }
        //判断是否需要手机验证
        ShopInfoDTO sdto = shopInfoService.selectById(dto.getShopId());
        if (sdto.getPayVerification().equals(0)){
            //若需要验证
            if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(captcha)){
                //手机号验证码为空，则返回参数错误
                return new Result<>(CodeEnum.FAIL_100002);
            }
            //验证验证码
            boolean flag = iUserLoginService.getSmsCaptcha(mobile, captcha, 4);
            if (!flag){
                return new Result<>(CodeEnum.FAIL_Login_1004);
            }
        }

        ArrayList<PayUserMoneySaveBO> saveBOS = new ArrayList<>();
        PayUserMoneySaveBO payUserMoneySaveBO = new PayUserMoneySaveBO();
        payUserMoneySaveBO.setUserId(dto.getShopId());
        payUserMoneySaveBO.setMoney(dto.getVacancyAmount());
        payUserMoneySaveBO.setUsage("余额-保证金");
        saveBOS.add(payUserMoneySaveBO);

        Result result = iPayUserRechargeService.saveFreezeMoney(saveBOS);
        if (result.getCode() == 0){
            ShopEarnestMoneyDTO newDto = new ShopEarnestMoneyDTO();
            newDto.setId(dto.getId());
            newDto.setStatus(1);
            newDto.setPaid(dto.getPaid().add(dto.getVacancyAmount()));//已缴纳
            newDto.setBalance(dto.getBalance().add(dto.getVacancyAmount()));//余额
            newDto.setVacancyAmount(new BigDecimal("0.00"));//需补足保证金
            newDto.setCountdownStatus(0);//倒计时状态 0未启动，1启动中，2已过期
            this.updateById(newDto);

            ShopInfoDTO shopInfoDTO = new ShopInfoDTO();
            shopInfoDTO.setId(dto.getShopId());
            shopInfoDTO.setEarnestMoney(newDto.getBalance());
            shopInfoDTO.setMarginStatus(1);//保证金状态 0未缴纳，1已缴纳，2保证金不足 defaultValue 0
            shopInfoService.updateById(shopInfoDTO);

            //增加保证金使用日志
            ShopEarnestMoneyLogDTO shopEarnestMoneyLogDTO = new ShopEarnestMoneyLogDTO();
            shopEarnestMoneyLogDTO.setMoney(dto.getVacancyAmount());
            shopEarnestMoneyLogDTO.setId(ymlConfig.getPkId());
            shopEarnestMoneyLogDTO.setShopId(dto.getShopId());
            shopEarnestMoneyLogDTO.setType(0);//类型 0缴纳、1扣除、2退回
            iShopEarnestMoneyLogService.insert(shopEarnestMoneyLogDTO);

            //杀掉保证金倒计时进程
            earnestMoneyTimerTaskService.remove(dto.getId().toString());
            //debug 解冻
            shopInfoService.autoUnfreeze(dto.getShopId());
        }
        return result;
    }

    @Override
    public Result updatePaidByOnline(Long id, String bankId) {
        ShopEarnestMoneyDTO dto = this.selectById(id);
        if (dto == null || dto.getStatus().equals(1) || dto.getVacancyAmount().compareTo(BigDecimal.ZERO) == 0){
            return new Result(CodeEnum.FAIL_200023);
        }
        PayUserRechargeSaveBO saveBO = new PayUserRechargeSaveBO();
        saveBO.setBankId(bankId);
        saveBO.setUserId(dto.getShopId());
        saveBO.setMoney(dto.getVacancyAmount());
//        saveBO.setType(2);//类型 0充值、2缴保证金
        saveBO.setUsage("网银-保证金");

        ShopPayAmountLogDTO shopPayAmountLogDTO = shopPayAmountLogService.getLast(dto.getShopId(), dto.getVacancyAmount(), 0);//类型 0保证金，1年费
        String paySerial = "";
        if (shopPayAmountLogDTO == null){
            //若没有生成新的。
            paySerial = ymlConfig.getPkId().toString();
            ShopPayAmountLogSaveBO shopPayAmountLogSaveBO = new ShopPayAmountLogSaveBO();
            shopPayAmountLogSaveBO.setPaySerial(paySerial);
            shopPayAmountLogSaveBO.setShopId(dto.getShopId());
            shopPayAmountLogSaveBO.setType(0);//类型 0保证金，1年费
            shopPayAmountLogSaveBO.setMoney(dto.getVacancyAmount());
            shopPayAmountLogService.add(shopPayAmountLogSaveBO);
        }else {
            paySerial = shopPayAmountLogDTO.getPaySerial();
        }

        saveBO.setBusinessId(Long.parseLong(paySerial));
        return iPayUserRechargeService.saveRechargeByOnlineBank(saveBO);
    }

    @Override
    public Result updatePaidReturn(ShopEarnestMoneyReturnBO bo) {
        //debug事务
        ShopPayAmountLogDTO shopPayAmountLogDTO = shopPayAmountLogService.getBySerial(bo.getPaySerial());
        if (shopPayAmountLogDTO == null){
            return  new Result<>(CodeEnum.FAIL);
        }
        //验证支付id
        if (StringUtils.isEmpty(shopPayAmountLogDTO.getPayId())){
            //若原记录没有支付id，则是第一次返回，更新数据
            ShopPayAmountLogDTO newDto = new ShopPayAmountLogDTO();
            newDto.setId(shopPayAmountLogDTO.getId());
            newDto.setStatus(1);
            newDto.setPayId(bo.getPayId());
            shopPayAmountLogService.updateById(newDto);
        }else {
            return new Result<>();
        }

        ShopInfoDTO shopInfoDTO = new ShopInfoDTO();
        shopInfoDTO.setId(bo.getShopId());

        ShopEarnestMoneyDTO oldDto = getByShopId(bo.getShopId());
        if (oldDto == null){
            return new Result<>(CodeEnum.FAIL);
        }

        ShopEarnestMoneyDTO newDto = new ShopEarnestMoneyDTO();
        newDto.setId(oldDto.getId());
        newDto.setStatus(1);
        newDto.setPaid(oldDto.getPaid().add(oldDto.getVacancyAmount()));//已缴纳
        newDto.setBalance(oldDto.getBalance().add(oldDto.getVacancyAmount()));//余额
        newDto.setVacancyAmount(new BigDecimal("0.00"));//需补足保证金
        newDto.setCountdownStatus(0);//倒计时状态 0未启动，1启动中，2已过期
        this.updateById(newDto);

        //杀掉保证金倒计时进程
        earnestMoneyTimerTaskService.remove(oldDto.getId().toString());

        shopInfoDTO.setEarnestMoney(newDto.getBalance());
        shopInfoDTO.setMarginStatus(1);//保证金状态 0未缴纳，1已缴纳，2保证金不足 defaultValue 0
        shopInfoService.updateById(shopInfoDTO);

        //添加日志
        //增加保证金使用日志
        ShopEarnestMoneyLogDTO shopEarnestMoneyLogDTO = new ShopEarnestMoneyLogDTO();
        shopEarnestMoneyLogDTO.setMoney(oldDto.getVacancyAmount());
        shopEarnestMoneyLogDTO.setId(ymlConfig.getPkId());
        shopEarnestMoneyLogDTO.setShopId(bo.getShopId());
        shopEarnestMoneyLogDTO.setType(0);//类型 0缴纳、1扣除、2退回
        iShopEarnestMoneyLogService.insert(shopEarnestMoneyLogDTO);

        //解冻
        shopInfoService.autoUnfreeze(bo.getShopId());

        return new Result<>();
    }

    @Override
    public Result updateReturnMoney(Long shopId) {
        ShopInfoDTO dto = shopInfoService.selectById(shopId);
        if (dto == null){
            return new Result<>(CodeEnum.FAIL_Login_1001);
        }
        //验证是否关店
        if (!dto.getAdminFrozenStatus().equals(2)){
            return new Result<>(CodeEnum.FAIL_100048);
        }

        ShopEarnestMoneyDTO oldDto = getByShopId(shopId);
        if (oldDto == null){
            return new Result<>(CodeEnum.FAIL_Login_1001);
        }

        //添加发起日志
        ShopEarnestMoneyLogDTO shopEarnestMoneyLogDTO = new ShopEarnestMoneyLogDTO();
        shopEarnestMoneyLogDTO.setMoney(oldDto.getVacancyAmount());
        shopEarnestMoneyLogDTO.setId(ymlConfig.getPkId());
        shopEarnestMoneyLogDTO.setShopId(shopId);
        shopEarnestMoneyLogDTO.setType(2);//类型 0缴纳、1扣除、2退回
        shopEarnestMoneyLogDTO.setNotes("发起保证金退还");
        iShopEarnestMoneyLogService.insert(shopEarnestMoneyLogDTO);

        //退换保证金，中金
        List<PayUserMoneySaveBO> list = new ArrayList<>();
        PayUserMoneySaveBO payUserMoneySaveBO = new PayUserMoneySaveBO();
        payUserMoneySaveBO.setUserId(shopId);
        payUserMoneySaveBO.setAccountType(1);
        payUserMoneySaveBO.setMoney(oldDto.getBalance());
        payUserMoneySaveBO.setUsage("退还-保证金");
        list.add(payUserMoneySaveBO);
        List<PayUserRechargeDTO> dtos = iPayUserRechargeService.saveUnfreezeMoney(list).getData();


        //添加完成日志
        ShopEarnestMoneyLogDTO endShopEarnestMoneyLogDTO = new ShopEarnestMoneyLogDTO();
        endShopEarnestMoneyLogDTO.setMoney(oldDto.getVacancyAmount());
        endShopEarnestMoneyLogDTO.setId(ymlConfig.getPkId());
        endShopEarnestMoneyLogDTO.setShopId(shopId);
        endShopEarnestMoneyLogDTO.setType(2);//类型 0缴纳、1扣除、2退回
        if (dtos != null && dtos.get(0).getStatus().equals(2)){
            //退还成功
            endShopEarnestMoneyLogDTO.setNotes("保证金退还成功");

            //保证金余额清0
            ShopInfoDTO shopInfoDTO = new ShopInfoDTO();
            shopInfoDTO.setId(shopId);
            shopInfoDTO.setEarnestMoney(BigDecimal.ZERO);
            shopInfoService.updateById(shopInfoDTO);

            ShopEarnestMoneyDTO shopEarnestMoneyDTO = new ShopEarnestMoneyDTO();
            shopEarnestMoneyDTO.setId(oldDto.getId());
            shopEarnestMoneyDTO.setBalance(BigDecimal.ZERO);

        }else {
            endShopEarnestMoneyLogDTO.setNotes("保证金退还失败");
            endShopEarnestMoneyLogDTO.setStatus(1);
        }

        iShopEarnestMoneyLogService.insert(shopEarnestMoneyLogDTO);

        return new Result<>();
    }



    private void andSms(Long shopId){
        //发送短信
        Wrapper<UserLoginDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", shopInfoService.selectById(shopId).getUserId());
        UserLoginDTO userLoginDTO = iUserLoginService.selectOne(wrapper);
        SmsInfoSaveBO smsInfoSaveBO = new SmsInfoSaveBO();
        smsInfoSaveBO.setMobile(userLoginDTO.getMobile());
        try {
            iSmsInfoService.saveEarnestMoney(smsInfoSaveBO);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //发送消息 只有站内信
    private void urgeSignContact(String conunt, Long sellShopId, Integer shopType) {
        MessageAnnouncementSaveBO messageAnnouncementSaveBO = new MessageAnnouncementSaveBO();

        if (shopType.equals(ShoppingCodeEnum.MERCHANT_BUYER.getCode())){
            //买家
            messageAnnouncementSaveBO.setShopType(MessageEnum.SHOP_TYPE_1.getCode());
        }else {
            messageAnnouncementSaveBO.setShopType(MessageEnum.SHOP_TYPE_2.getCode());
        }

        ResponseDTO responseDTO = new ResponseDTO();
        messageAnnouncementSaveBO.setContent(conunt);
        messageAnnouncementSaveBO.setShopId(sellShopId);
        messageAnnouncementSaveBO.setType(MessageEnum.SYS_MESSAGE.getCode());
        messageAnnouncementSaveBO.setTitle("保证金扣除通知");
        messageAnnouncementSaveBO.setJumpType(MessageEnum.MESSAGE_SHOW.getCode() + "");
        messageAnnouncementSaveBO.setTextType(1);
        messageAnnouncementSaveBO.setKey(sellShopId+"");
        responseDTO.setData(messageAnnouncementSaveBO);
        responseDTO.setMessageId(sellShopId + "");
        kafkaProducerService.senMessage(JSON.toJSONString(responseDTO), MessageTopicType.COMMENT_MESSAGE_TOPIC);
    }
}
